<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-5-27
 * Time: 10:43
 */

namespace zhtfrontend\controllers;

use common\models\datas\Pta;
use zhtfrontend\components\bases\BaseController;
use yii\web\Response;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use yii\web\BadRequestHttpException;
use yii\data\Pagination;
use yii\helpers\Json;
use doumiaoduo\buildform\BuildForm;

class PtaController extends BaseController
{
    public function actionIndex()
    {
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $data = Pta::find()->where(['site_id' => Fun::getSiteId()])->orderBy('id ASC');
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($model, ['zhtfrontend_operate', 'customer_name']);

        return $this->render('index',['data'=>$array,'pages'=>$pages]);
    }


    public function actionUpdate()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $info = Pta::findOne(\Yii::$app->request->get('id'));
        if ($info === null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $info->customer_id = isset($info->customer) ? $info->customer->name : '';
        $form = new BuildForm('@app/components/forms/ptaForm.php');
        $form->loadModel(['pta' => $info]);
        $form->setOptions('pta', 'customer_id', ['disabled' => 'disabled']);
        $form->setOptions('pta', 'apply_time', ['disabled' => 'disabled']);
        $form->setOptions('pta', 'apply_reasons', ['disabled' => 'disabled']);
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $model = $form->getModel('pta');
            $info->check_time = time();
            $result = $model->states;
            $info->user_id = \Yii::$app->user->id;
            if ($result) {
                $info->created_at = time();
            }
            unset($info->customer_id);
            if ($info->save(false)) {
                return $this->redirectMessage(\Yii::t('zhtfrontend', '审核处理完成'), Fun::url(['pta/index']), 'success');
            }
        }
        return $this->render('verificat', ['form' => $form->render()]);
    }
}




